package com.example.lib_common.base

import android.os.Bundle
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.example.lib_common.R
import com.example.lib_common.observeWithLifecycle
import com.example.lib_common.state.UIState
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

/**
 * @author yaotianxue
 * @description
 * @date 2025/1/15
 */
abstract class BaseFragment<VM:BaseViewModel,VDB: ViewDataBinding>: Fragment() {
    open val mPageCanBack:Boolean = true
    val mCanBack:Boolean //fragment嵌套
        get() {
            //可返回
            if(mPageCanBack){
                return true
            }
            //不可返回
            val fragments = childFragmentManager.fragments //当前fragment是否有子fragment
            if(fragments.isEmpty()){
                return false
            }
            for (fragment in fragments) {
                if(fragment is BaseFragment<*,*>){
                   if(fragment.isVisible){
                       return fragment.mPageCanBack
                   }
                }
            }
            return mPageCanBack
        }

    protected abstract val mLayoutResId:Int //布局id
    protected abstract val mViewModel:VM//viewmodel

    protected lateinit var mBinding: VDB //数据绑定
    protected lateinit var mParentView: View //view


    override fun onCreateView(
        inflater: android.view.LayoutInflater,
        container: android.view.ViewGroup?,
        savedInstanceState: android.os.Bundle?
    ): android.view.View? {
        mBinding = DataBindingUtil.inflate(layoutInflater, mLayoutResId, container, false)
        mBinding.lifecycleOwner = viewLifecycleOwner
        mParentView = mBinding.root
        return mParentView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
        initListener()
        initData()
    }
    abstract fun initView()
    abstract fun initData()
    abstract fun initListener()

    /**
     * 隐藏键盘
     */
    fun hideSoftKeyBroad(view:View){
        if(activity is BaseActivity<*,*>){
            (requireActivity() as BaseActivity<*,*>).hideKeyBroad(view)
        }
    }

    /**
     * 显示键盘
     */
    fun showSoftKeyBroad(view:View){
        if(activity is BaseActivity<*,*>){
            (requireActivity() as BaseActivity<*,*>).showKeyBroad(view)
        }
    }

    fun setLoadingViewVisible(show:Boolean,msg:String = ""){
        if(activity is BaseActivity<*,*>){
            (requireActivity() as BaseActivity<*,*>).setLoadingViewVisible(show,msg)
        }

    }

    /**
     * 通用状态监听：Loading显示和隐藏、错误提示
     */
    open fun addCommonStateListener(state: StateFlow<UIState>){
        state.observeWithLifecycle(this){
            when(it){
                is UIState.Loading -> {
                    setLoadingViewVisible(true)
                }
                is UIState.Error -> {
                    setLoadingViewVisible(false)
                    Toast.makeText(requireContext(),it.message, Toast.LENGTH_SHORT)
                }
                is UIState.Success -> {
                    setLoadingViewVisible(false)
                }
                else -> {
                }
            }
        }
    }




}